﻿﻿/**  Ccay IGrid PopupEditor
 *
 */
(function(IGrid, Const) {
	/** 扩展IGrid的manager管理器 */
	Ccay.UI.XControl.extendIManager("XGrid", {
		/** 打开弹出编辑器
		 * @param   可以为
		 */
		popupEdit : function(param,mode){
			if(param){
				if("rowIdx" in param){
					record = param;
				}else if(param.jquery &&  param.is("tr")){
					record = this.getRowData(param);
				}else{
					record = this.op.Storage.findOne(param);
				}
				PopupEditor.popup(this, record,mode || "edit");
				
			}else{
				PopupEditor.popup(this,null,"create");
			}
		}
	});
	
	
	var defaultOp = {
	
		content: "<div class='igrid-edit-panel'></div>",
		imgBtn : true,
		tabId : "defaultTabId",
		showFoot : false,
		autoRefresh : true,
		xFormOp : {
			renderType : "edit",
			layout: "form-Flow",
			showHead : false
		},
		create :{
			xFormOp : {
				buttons : [{text :"ccay.common.label.save", css : "ccay-button",icon :'confirm', click : function(e,form, record, grid,popupOp){
					if(form.valid()){
						PopupEditor.save(form.getValue(), record, grid, this,popupOp);
					}
				}},{text :"ccay.common.button.cancel", css : "ccay-button",icon :'cancel', click : function(){
					this.close ? this.close() : this.removeItem(this.op.tabId);
				}}]
			}
		},
		edit :{
			xFormOp : {
				buttons : [{text :"ccay.common.label.save", css : "ccay-button",icon :'confirm', click : function(e,form, record, grid,popupOp){
					if(form.valid()){
						PopupEditor.save(form.getValue(), record, grid, this,popupOp);
					}
				}},{text :"ccay.common.button.cancel", css : "ccay-button",icon :'cancel', click : function(){
					this.close ? this.close() : this.removeItem(this.op.tabId);
				}}]
			}
		},
		view :{
			xFormOp : {
				renderType : "view" ,
				layout: "form-Flow",
				buttons : [{text :"ccay.common.label.close",css : "ccay-button", icon :'cancel', click : function(){
					this.close ? this.close() : this.removeItem(this.op.tabId);
				}}]
			}

		}
	}

	var PopupEditor = IGrid.PopupEditor = {

		/**
		 * @description 设置列的编辑器配置
		 * @param {Object}
		 *            grid IGrid对象
		 * @param {Object}
		 *            col 列的配置信息
		 * @param {JsonObject/Array/String}
		 *            field 属性值
		 * @return {IGrid}
		 */
		setCol : function(grid, col) {
			// 过滤：内置列、列类型、不能编辑列
			if (!col.inner && !col.columnType && col.editable !== false) {
				// 列的编辑属性赋值
				col.editorOp = $.extend({
							type : col.type,
							field : col.valueField || col.field,
							format : col.format,
							rule : col.rule
						}, col.editorOp);
				if (!col.editor || typeof(col.editor) == "string") {
					// 通过type类型创建编辑器
					col.editor = Ccay.Editor.get(col.editor || col.editorOp.type);
				}
				var newCol = $.extend({},col);
				newCol.field = newCol.valueField || newCol.field;
				grid.op.modalOp.fields.push(newCol);
			}
		},
		
		popup : function(grid, record, type){
			var op = grid.op, widget;
			if(!op.modal){
				op.modal =  Ccay.Modal.create(op.modalOp);
			}
			var popupOp = $.extend(true,{},defaultOp, op.popupEditor);
			popupOp = $.extend(true,{},popupOp, popupOp[type],{xFormOp:{modal : op.modal,dataset:record}});
			
			var fnInitXForm = function(popupOp,panel){
				var xform = panel.xForm(popupOp.xFormOp);
				popupOp.afterLoad && popupOp.afterLoad(xform,dialog);
				if(record){
					 if( !popupOp.beforeSetValue || popupOp.beforeSetValue(record, xform, dialog) !== false){
						 xform.setValue(record);
					 }
				}
			}
			
			var buttons = popupOp.xFormOp.buttons;
			buttons && $.each(buttons,function(i, btn){
				!btn._click && (btn._click =  btn.click);
				btn.click =  function(e){
					var xform =widget.dom.find("div.igrid-edit-panel:first").xForm();
					btn._click && btn._click.call(widget, e, xform, record, grid, popupOp);
				}
			})
			if(popupOp.tab){
				var widget = $(popupOp.tab).manager();
				widget.addItem(popupOp);
				fnInitXForm(popupOp,widget.dom.find("div[tabid='"+popupOp.tabId+"']").find("div.igrid-edit-panel"))
			}else{
				var dialog = Ccay.UI.dialog(null, popupOp.title, popupOp);
				fnInitXForm(popupOp,dialog.find("div.igrid-edit-panel"));
				widget = dialog.manager();
			    widget.fixPosition();
			}
		},
		
		
		save : function(newData,data, grid, widget,popupOp){
			if(!popupOp.beforeSave || popupOp.beforeSave(newData,data, grid, widget) !== false){
				if(!popupOp.autoRefresh){
					if(data){
						grid.op.Storage.updateItem(data,newData);
						var tr = grid.getRow(data.rowIdx);
						grid.renderRow(tr, grid.getRowData(tr));
					}else{
						grid.appendRow([newData])
					}
				}
				if(popupOp.editType != "draft"){
					if(popupOp.save){
						popupOp.save(newData,data, grid, widget);
						widget.close ? widget.close() : this.removeItem(widget.op.tabId);
						return;
					}
					var ajaxOp = $.extend( {type : "POST", data : $.extend({},data,newData), success : function(){
						if(popupOp.autoRefresh){
							grid.reload(false,true);
						}
						widget.close ? widget.close() : widget.removeItem(widget.op.tabId);
						popupOp.afterSave && popupOp.afterSave(newData,data, grid);
					}},popupOp.saveAjax);
					Ccay.Core.ajax(ajaxOp,widget.getBody ? widget.getBody() : widget.dom);
				}

			}
			
		}
		
		
	}
	/** 注册编辑器插件 */
	Ccay.UI.XControl.addPlugin("XGrid", "popupEditor", {
			preInit : function(grid, op){
				if (typeof op.popupEditor == "object") {
					if(!op.operation){
						op.operation = {insert : {}};
					}else if(!op.operation.insert){
						op.operation.insert = {};
					}
				}
			},
			build : function(grid, op) {
				var handler = op.handler;
				if (typeof op.popupEditor == "object") {
					//如果没有设置modal，则通过column设置，创建一个modal
					if(!op.modalOp || !op.modalOp.fields){
						op.modalOp =  {
							autoAlias : true,
							modalName : "modal",
							fields : []
						};
						handler.setColumn.add(PopupEditor.setCol);
					}
					op.handler.click.add("popupedit", function(e, trigger, grid){
						grid.popupEdit( $.getEventTarget(e).parents("tr:first"),"edit");
					});
					op.handler.click.add("popupview", function(e, trigger, grid){
						grid.popupEdit( $.getEventTarget(e).parents("tr:first"),"view");
					});
				}
			}
		});

})(Ccay.UI.IGrid, Ccay.UI.IGrid.Const);
